
#include "led.h"

//static volatile unsigned int *CCM_CCGR1                              ;
static volatile unsigned int *CCM_CCGR2                              ;
//static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;
static volatile unsigned int *IOMUXC_SW_MUX_CTL_PAD_LCD_CLK;
//static volatile unsigned int *GPIO5_GDIR                             ;
static volatile unsigned int *GPIO3_GDIR                             ;
//static volatile unsigned int *GPIO5_DR                               ;
static volatile unsigned int *GPIO3_DR                               ;

/**********************************************************************
 * 函数名称： led_init
 * 功能描述： 初始化LED引脚，就是把它设置为输出引脚
 ***********************************************************************/
void led_init(void)
{
	unsigned int val;
	
	//CCM_CCGR1                               = (volatile unsigned int *)(0x20C406C);
	CCM_CCGR2                               = (volatile unsigned int *)(0x20C4070);
	//先看的原理图，知道对应GPIO3，找到它对应的是CCM_CCGR2，找到对应的地址0x20C4070

	//IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = (volatile unsigned int *)(0x2290014);
	IOMUXC_SW_MUX_CTL_PAD_LCD_CLK = (volatile unsigned int *)(0x20E0104);
	//直接搜索GPIO3一个一个找的

	//GPIO5_GDIR                              = (volatile unsigned int *)(0x020AC000 + 0x4);
	GPIO3_GDIR                              = (volatile unsigned int *)(0x020A4000 + 0x4);
	//看芯片手册第二章来找寄存器地址

	//GPIO5_DR                                = (volatile unsigned int *)(0x020AC000);
	GPIO3_DR                                = (volatile unsigned int *)(0x020A4000);
	//和上面相同

	/* GPIO3_IO00 */
	/* a. 使能GPIO3
	 * set CCM to enable GPIO3
	 * CCM_CCGR2[CG13] 0x20C4070
	 * bit[27:26] = 0b11
	 */
	*CCM_CCGR2 |= (3<<26);
	
	/* b. 设置GPIO5_IO00用于GPIO
	 * set IOMUXC_SW_MUX_CTL_PAD_LCD_CLK
	 *      to configure GPIO3_IO00 as GPIO
	 * IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3  0x20E0104
	 * bit[3:0] = 0b0101 alt5
	 */
	val = *IOMUXC_SW_MUX_CTL_PAD_LCD_CLK;
	val &= ~(0xf);
	val |= (5);
	*IOMUXC_SW_MUX_CTL_PAD_LCD_CLK = val;
	
	
	/* c. 设置GPIO3_IO00作为output引脚
	 * set GPIO3_GDIR to configure GPIO3_IO00 as output
	 * GPIO3_GDIR  0x020A4000 + 0x4
	 * bit[3] = 0b1
	 */
	*GPIO3_GDIR |= (1<<3);

}

/**********************************************************************
 * 函数名称： led_ctl
 * 功能描述： 设置LED状态
 * 输入参数： 
 *     on : 1-LED点亮, 0-LED熄灭
 ***********************************************************************/
void led_ctl(int on)
{
	if (on) /* on: output 0*/
	{
		/* d. 设置GPIO3_DR输出低电平
		 * set GPIO3_DR to configure GPIO3_IO00 output 0
		 * GPIO3_DR 0x020A4000 + 0
		 * bit[3] = 0b0
		 */
		*GPIO3_DR &= ~(1<<3);
	}
	else  /* off: output 1*/
	{
		/* e. 设置GPIO3_IO0输出高电平
		 * set GPIO3_DR to configure GPIO3_IO00 output 1
		 * GPIO5_DR 0x020A4000 + 0
		 * bit[3] = 0b1
		 */ 
		*GPIO3_DR |= (1<<3);
	}
}




	
	
